/**
Copyright (c) 2011  Nicholas Zhu

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 */
package com.cardgroup.cbrp.springmem.aop;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.InvalidParameterException;

import org.aspectj.lang.JoinPoint;

import com.cardgroup.cbrp.springmem.annotations.ReadStaticCache;

/**
 * Build for static case
 * Don't need group
 * @author nicholas
 *
 */
public class StaticCacheAnnotationDataBuilder extends AnnotationDataBuilder
		implements IAnnotationDataBuilder {

	public StaticCacheAnnotationDataBuilder(Class expectedAnnotationClass,
			JoinPoint jp) {
		super(expectedAnnotationClass, jp);
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public void buildAnnotation() {
		// TODO Auto-generated method stub		
		try{
			super.buildAnnotation();
			final Class clazz = annotation.annotationType();
			if (expectedAnnotationClass == ReadStaticCache.class) {
				final Object[] args = jp.getArgs();
				StringBuffer strArgs = new StringBuffer();
				if ((args != null) && (args.length != 0)) {
					for (int i = 0; i < args.length; i++) {
						if (args[i] == null) {
							strArgs.append(KEY_SEPARATOR);
						} else {
							strArgs.append(args[i]).append(KEY_SEPARATOR);
						}
					}
				}
				data.setParameters(strArgs.toString());
			}

			if (expectedAnnotationClass == ReadStaticCache.class) {
				final Method expirationMethod = clazz.getDeclaredMethod(
						"expiration", null);
				final int expiration = (Integer) expirationMethod.invoke(
						annotation, null);
				if (expiration < 0) {
					throw new InvalidParameterException(
							String.format(
									"Expiration for annotation [%s] must be 0 or greater on [%s]",
									expectedAnnotationClass.getName(),
									methodToCache));
				}
				data.setExpiration(expiration);
			}

		} catch (NoSuchMethodException ex) {
			throw new RuntimeException(
					"Problem assembling Annotation information.", ex);
		} catch (IllegalAccessException ex) {
			throw new RuntimeException(
					"Problem assembling Annotation information.", ex);
		} catch (InvocationTargetException ex) {
			throw new RuntimeException(
					"Problem assembling Annotation information.", ex);
		}
	}
	@Override
	public void buildGroupKey() {
		// TODO Auto-generated method stub
		// nothing, no group key
	}
}
